//
//  HXDMainViewController.swift
//  weibo
//
//  Created by huxuedong on 15/12/5.
//  Copyright © 2015年 huxuedong. All rights reserved.
//

import UIKit
import SVProgressHUD

class HXDMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let tabBar = HXDTabBar()
        //解除循环引用
        tabBar.plusButtonClosure = {[weak self] in
            if !HXDUserAccountViewModel.shareInstance.userLogin {
                SVProgressHUD.showErrorWithStatus("请先登录！")
                return
            }
            let plusView = HXDPlusView()
            //将plusView显示到当前控制器上
            plusView.show(self)
        }
        //tabBar是UITabBarController的属性，通过KVC设置自定义tabBar
        setValue(tabBar, forKey: "tabBar")
        addChildViewController(HXDHomeTableViewController(), imageName: "tabbar_home", title: "首页");
        addChildViewController(HXDMessageTableViewController(), imageName: "tabbar_message_center", title: "消息")
        addChildViewController(HXDDiscoverTableViewController(), imageName: "tabbar_discover", title: "发现")
        addChildViewController(HXDProfileTableViewController(), imageName: "tabbar_profile", title: "我")
    }
    
    //自定义添加控制器的方法
    func addChildViewController(childController: UIViewController, imageName: String, title: String) {
        //设置tabBarItem为自定义HXDTabBarItem
        childController.tabBarItem = HXDTabBarItem()
        //统一设置tabBar文字颜色
//        let tabBar = UITabBar.appearance()
//        tabBar.tintColor = UIColor.orangeColor()
//        childController.tabBarItem.title = title
//        childController.navigationItem.title = title
        childController.title = title
        childController.tabBarItem.image = UIImage(named: imageName)
        //设置图片渲染方式（原样显示）
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        //设置字体颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: .Selected)
        //设置字体大小
//    childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFontOfSize(15)], forState: .Normal)
        //设置图标位置
//        childController.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0)
        addChildViewController(HXDNavigationController(rootViewController: childController))
    }

}
